package com.xiyuan.Entity2Json;

import com.xiyuan.Entity2Json.bean.JsonResult;

import java.lang.reflect.Field;

/**
 * 默认操作，只将当前对象转为json
 */
public class DefaultEntity2JsonStrategy extends AbstractEntity2JsonStrategy {

    /**
     * 产生基础的jsonObject
     */
    public JsonResult toJsonString(Class<?> clazz) {

//        反射获取字段
        Field[] fields = clazz.getDeclaredFields();
        JsonResult jsonResult = new JsonResult();
        for (Field field : fields) {
            setValue(jsonObject, field);
            jsonResult.addFieldToList(field.getName());
        }
        jsonResult.setJsonString(jsonObject.toString());
        return jsonResult;
    }

}
